package com.zd.mole.site.mohurd.process;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zd.mole.process.ProcessHandler;
import com.zd.mole.site.mohurd.entity.Ot_contract_info;
import com.zd.mole.site.mohurd.repository.OtCompanyInfoRepository;
import com.zd.mole.site.mohurd.service.SysDictService;
import com.zd.mole.task.TaskStatus;
import com.zd.mole.task.entity.Task;
import com.zd.mole.task.service.TaskService;
import com.zd.mole.utils.RegexUtils;

/**
 * 项目数据/合同备案
 * @author ZhangDi
 *
 */
@Component
@Transactional
public class MohurdDataserviceQueryProjectContractInfoHandler implements ProcessHandler {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@PersistenceContext 
	private EntityManager em;
	@Resource
	private SysDictService sysDictService;
	@Resource
	private OtCompanyInfoRepository otCompanyInfoRepository;
	@Resource
	private TaskService taskService;
	
	@Override
	public List<Task> handler(Task task, String text) {
		Ot_contract_info c = new Ot_contract_info();
		String id = task.getRequestUrl().replaceFirst("/dataservice/query/project/contractInfo/", "");
		c.setId(id);
		c.setProjectNo(task.getCode());
		c.setBakCode(RegexUtils.find("<td data-header=\"合同备案编号\">" + INNER_HTML + "</td>", text));
		c.setProBakCode(RegexUtils.find("<td data-header=\"省级合同备案编号\">" + INNER_HTML + "</td>", text));
		c.setContractCode(RegexUtils.find("<td data-header=\"合同编号\">" + INNER_HTML + "</td>", text));
		String contractType = RegexUtils.find("<td data-header=\"合同分类\">" + INNER_HTML + "</td>", text);
		contractType = sysDictService.findOrSaveByTypeLabel("oLife_ProContractType", contractType, "合同分类");
		c.setContractType(contractType);
		c.setContractGroup(RegexUtils.find("<td data-header=\"合同类别\">" + INNER_HTML + "</td>", text));
		c.setContractSum(RegexUtils.find("<td data-header=\"合同金额（万元）\">" + INNER_HTML + "</td>", text));
		c.setDevlopScope(RegexUtils.find("<td colspan=\"3\" data-header=\"建设规模\">" + INNER_HTML + "</td>", text));
		String contractDate = RegexUtils.find("<td colspan=\"3\" data-header=\"合同签订日期\">" + INNER_HTML + "</td>", text);
		if(!"0001-01-01".equals(contractDate) && !"1900-01-01".equals(contractDate)) {
			try {
				c.setContractDate(new SimpleDateFormat("yyyy-MM-dd").parse(contractDate));
			} catch (ParseException e) {
				log.warn("日期转换失败 {}", e.getMessage());
			}
		}
		String fapackUnitNo = RegexUtils.find("<td data-header=\"发包单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text);
		c.setFapackUnitNo(fapackUnitNo);
		if("".equals(fapackUnitNo)) {
			c.setFapackUnitName(RegexUtils.find("<td data-header=\"发包单位名称\">\\s*" + INNER_HTML + "\\s*</td>", text));
		} else {
			c.setFapackUnitName(RegexUtils.find("<td data-header=\"发包单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text));
			if(!otCompanyInfoRepository.existsById(fapackUnitNo)) {
				Task nt = new Task();
				nt.setCode(fapackUnitNo);
				nt.setHostUrl(task.getHostUrl());
				nt.setRequestUrl("/dataservice/query/comp/compDetail/" + fapackUnitNo);
				nt.setProcessHandlerClassName("MohurdDataserviceQueryCompCompDetailHandler");
				nt.setStatus(TaskStatus.Ready);
				taskService.save(nt);
			};
		}
		c.setFapackUnitCode(RegexUtils.find("<td data-header=\"发包单位组织机构代码\">" + INNER_HTML + "</td>", text));
		String chengpackUnitNo = RegexUtils.find("<td data-header=\"承包单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text);
		c.setChengpackUnitNo(chengpackUnitNo);
		if("".equals(chengpackUnitNo)) {
			c.setChengpackUnitName(RegexUtils.find("<td data-header=\"承包单位名称\">\\s*" + INNER_HTML + "\\s*</td>", text));
		} else {
			c.setChengpackUnitName(RegexUtils.find("<td data-header=\"承包单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text));
			if(!otCompanyInfoRepository.existsById(chengpackUnitNo)) {
				Task nt = new Task();
				nt.setCode(chengpackUnitNo);
				nt.setHostUrl(task.getHostUrl());
				nt.setRequestUrl("/dataservice/query/comp/compDetail/" + chengpackUnitNo);
				nt.setProcessHandlerClassName("MohurdDataserviceQueryCompCompDetailHandler");
				nt.setStatus(TaskStatus.Ready);
				taskService.save(nt);
			};
		}
		c.setChengpackUnitCode(RegexUtils.find("<td data-header=\"承包单位组织机构代码\">" + INNER_HTML + "</td>", text));
		String unionpackUnitNo = RegexUtils.find("<td data-header=\"联合体承包单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text);
		c.setUnionpackUnitNo(unionpackUnitNo);
		if("".equals(unionpackUnitNo)) {
			c.setUnionpackUnitName(RegexUtils.find("<td data-header=\"联合体承包单位名称\">\\s*" + INNER_HTML + "\\s*</td>", text));
		} else {
			c.setUnionpackUnitName(RegexUtils.find("<td data-header=\"联合体承包单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text));
			if(!otCompanyInfoRepository.existsById(unionpackUnitNo)) {
				Task nt = new Task();
				nt.setCode(unionpackUnitNo);
				nt.setHostUrl(task.getHostUrl());
				nt.setRequestUrl("/dataservice/query/comp/compDetail/" + unionpackUnitNo);
				nt.setProcessHandlerClassName("MohurdDataserviceQueryCompCompDetailHandler");
				nt.setStatus(TaskStatus.Ready);
				taskService.save(nt);
			};
		}
		c.setUnionpackUnitCode(RegexUtils.find("<td data-header=\"联合体承包单位组织机构代码\">" + INNER_HTML + "</td>", text));
		c.setCreate_date(new Date());
		String recordDate = RegexUtils.find("<td colspan=\"3\" data-header=\"记录登记时间\">" + INNER_HTML + " </td>", text);
		try {
			c.setRecordDate(new SimpleDateFormat("yyyy-MM-dd").parse(recordDate));
			c.setCreate_date(c.getRecordDate());
		} catch (ParseException e) {
			log.warn("日期转换失败 {}", e.getMessage());
		}
		c.setCreate_by("1");
		c.setUpdate_by("1");
		c.setUpdate_date(new Date());
		c.setDel_flag("0");
		em.merge(c);
		return null;
	}

}
